// UniApp 消息提示工具类，模仿 Element UI 的 el-message 组件

type MessageType = 'success' | 'error' | 'warning' | 'info';

interface MessageOptions {
  content: string;
  type?: MessageType;
  duration?: number;
  icon?: string;
}

class UniMessageUtil {
  private static instance: UniMessageUtil;

  private constructor() {}

  public static getInstance(): UniMessageUtil {
    if (!UniMessageUtil.instance) {
      UniMessageUtil.instance = new UniMessageUtil();
    }
    return UniMessageUtil.instance;
  }

  /**
   * 显示消息
   * @param options 消息选项
   */
  public show(options: string | MessageOptions): void {
    const defaultOptions: MessageOptions = {
      content: '',
      type: 'info',
      duration: 3000
    };

    const messageOptions: MessageOptions = typeof options === 'string' 
      ? { ...defaultOptions, content: options } 
      : { ...defaultOptions, ...options };

    // 根据不同类型设置图标和样式
    const typeConfig = {
      success: { icon: 'success', iconColor: '#67C23A' },
      error: { icon: 'error', iconColor: '#F56C6C' },
      warning: { icon: 'warning', iconColor: '#E6A23C' },
      info: { icon: 'info', iconColor: '#909399' }
    };

    const config = typeConfig[messageOptions.type || 'info'];

    // 使用 uni.showToast 显示消息
    uni.showToast({
      title: messageOptions.content,
      icon: messageOptions.type === 'error' ? 'error' : 'success',
      duration: messageOptions.duration || 3000
    });
  }

  /**
   * 显示成功消息
   * @param content 消息内容
   * @param duration 持续时间(毫秒)
   */
  public success(content: string, duration?: number): void {
    this.show({ content, type: 'success', duration });
  }

  /**
   * 显示错误消息
   * @param content 消息内容
   * @param duration 持续时间(毫秒)
   */
  public error(content: string, duration?: number): void {
    this.show({ content, type: 'error', duration });
  }

  /**
   * 显示警告消息
   * @param content 消息内容
   * @param duration 持续时间(毫秒)
   */
  public warning(content: string, duration?: number): void {
    this.show({ content, type: 'warning', duration });
  }

  /**
   * 显示普通消息
   * @param content 消息内容
   * @param duration 持续时间(毫秒)
   */
  public info(content: string, duration?: number): void {
    this.show({ content, type: 'info', duration });
  }

  /**
   * 显示模态框类型的消息（带确认按钮）
   * @param options 消息选项
   */
  public alert(options: string | MessageOptions): void {
    const defaultOptions: MessageOptions = {
      content: '',
      type: 'info',
      duration: 0
    };

    const messageOptions: MessageOptions = typeof options === 'string' 
      ? { ...defaultOptions, content: options } 
      : { ...defaultOptions, ...options };

    // 使用 uni.showModal 显示带确认按钮的消息
    uni.showModal({
      title: this.getTitleByType(messageOptions.type),
      content: messageOptions.content,
      showCancel: false,
      confirmColor: this.getColorByType(messageOptions.type)
    });
  }

  /**
   * 显示确认框类型的消息（带确认和取消按钮）
   * @param options 消息选项
   * @param confirmCallback 确认回调函数
   * @param cancelCallback 取消回调函数
   */
  public confirm(options: string | MessageOptions, 
                 confirmCallback?: () => void, 
                 cancelCallback?: () => void): void {
    const defaultOptions: MessageOptions = {
      content: '',
      type: 'info',
      duration: 0
    };

    const messageOptions: MessageOptions = typeof options === 'string' 
      ? { ...defaultOptions, content: options } 
      : { ...defaultOptions, ...options };

    // 使用 uni.showModal 显示带确认和取消按钮的消息
    uni.showModal({
      title: this.getTitleByType(messageOptions.type),
      content: messageOptions.content,
      showCancel: true,
      confirmColor: this.getColorByType(messageOptions.type),
      success: (res) => {
        if (res.confirm) {
          confirmCallback && confirmCallback();
        } else if (res.cancel) {
          cancelCallback && cancelCallback();
        }
      }
    });
  }

  private getTitleByType(type?: MessageType): string {
    const titles = {
      success: '成功',
      error: '错误',
      warning: '警告',
      info: '提示'
    };
    return titles[type || 'info'];
  }

  private getColorByType(type?: MessageType): string {
    const colors = {
      success: '#67C23A',
      error: '#F56C6C',
      warning: '#E6A23C',
      info: '#409EFF'
    };
    return colors[type || 'info'];
  }
}

export default UniMessageUtil.getInstance();

// 为了方便使用，导出常用方法
export const uniMessage = UniMessageUtil.getInstance();
export const uniSuccess = (content: string, duration?: number) => uniMessage.success(content, duration);
export const uniError = (content: string, duration?: number) => uniMessage.error(content, duration);
export const uniWarning = (content: string, duration?: number) => uniMessage.warning(content, duration);
export const uniInfo = (content: string, duration?: number) => uniMessage.info(content, duration);